#include "..\\stdafx.h"
#include "MainFrame.h"
#include "SecurityList.h"
#include "..\\Service\\StockService.h"
#include "LatestDiv.h"

static MainFrame *m_mainFrame = 0;

MainFrame::MainFrame(){
    m_requestID1 = -1;
    m_requestID2 = -1;
    m_mainFrame = this;
    m_tabFunc = 0;
}

MainFrame* MainFrame::getMainFrame(){
    return m_mainFrame;
}

void MainFrame::loadFile(const String& fileName, FCView *view){
    FCUIXml::loadFile(fileName, 0);
    m_tabFunc = (FCTabView*)findView(L"tabFunc");
    m_tabFunc->addEvent((FCInvokeEventCallBack*)this, L"oninvoke", this);
    startConnect();
}

void MainFrame::changeTheme()
{
    int nowStyle = MyColor::getStyle();
    if (nowStyle == 0)
    {
        nowStyle = 1;
    }
    else
    {
        nowStyle = 0;
    }
    MyColor::setStyle(nowStyle);
}

void MainFrame::startConnect(){
    SecurityList *securityList = (SecurityList*)(getNative()->findView(L"gridStocks"));
    securityList->firstSearchCode();
    searchCode(L"600000.SH");
}

void MainFrame::searchCode(String code){
    if (m_requestID1 != -1)
    {
        DataCenter::getLatestServiceClient()->removeListener(m_requestID1);
        DataCenter::getLatestServiceClient()->unSubCodes(m_requestID1);
    }
    int oldReqID = m_requestID2;
    if (m_requestID2 != -1)
    {
        DataCenter::getHistoryServiceClient()->removeListener(m_requestID2);
    }
    m_requestID1 = FCClientService::getRequestID();
    m_requestID2 = FCClientService::getRequestID();
    for (int i = 0; i < m_charts.size(); i++)
    {
        MyChart *myChart = m_charts.get(i);
        myChart->m_code = code;
        FCChartEx *chart = (FCChartEx*)myChart->m_chart;
        myChart->m_subscription = chart->getSubScription();
        if (myChart->m_cycle == 0)
        {
            chart->m_subComboBox->setVisible(false);
        }
        else
        {
            Security security;
            if (DataCenter::getSecurityService()->getSecurityByCode(myChart->m_code, &security))
            {
                if (security.m_type > 0)
                {
                    chart->m_subComboBox->setVisible(true);
                }
                else
                {
                    chart->m_subComboBox->setVisible(false);
                }
            }
            else
            {
                chart->m_subComboBox->setVisible(false);
            }
        }
    }
    m_latestData.m_requestID = -1;
    DataCenter::getLatestServiceClient()->addListener(m_requestID1, this, 0);
    DataCenter::getHistoryServiceClient()->addListener(m_requestID2, this, 0);
    DataCenter::getLatestServiceClient()->subCodes(m_requestID1, code);
    DataCenter::getHistoryServiceClient()->reqData2(m_requestID2, code, oldReqID);
    m_lastCode = code;
}

void MainFrame::callListenerMessageEvent(Object sender, FCMessage *message, Object invoke){
    FCMessage *copyMessage = new FCMessage;
    copyMessage->copy(message);
    char *str = new char[copyMessage->m_bodyLength];
    for(int i = 0; i < copyMessage->m_bodyLength; i++){
        str[i] = message->m_body[i];
    }
    copyMessage->m_body = str;
    m_tabFunc->invoke(copyMessage);
}

void MainFrame::bindData(FCMessage *message){
    if (message->m_functionID == FUNCTIONID_GETDATA2)
    {
        if (message->m_requestID == m_requestID2)
        {
            String code = L"";
            ArrayList<SecurityData> dayDatas;
            ArrayList<SecurityData> minuteDatas;
            ArrayList<SecurityData> trendDatas;
            HistoryServiceClient::getDatas2(&code, &dayDatas, &minuteDatas, &trendDatas, message->m_body, message->m_bodyLength);

            bool hasAfter = false;
            for (int i = 0; i < m_charts.size(); i++)
            {
                MyChart *myChart = m_charts.get(i);
                if (myChart->m_cycle > 0)
                {
                    if (myChart->m_subscription == 2)
                    {
                        hasAfter = true;
                    }
                }
            }
            ArrayList<SecurityData> beforeDatas;
            ArrayList<SecurityData> afterDatas;
            for (int i = 0; i < dayDatas.size(); i++)
            {
                SecurityData data = dayDatas.get(i);
                SecurityData fData;
                fData = data;
                beforeDatas.add(fData);
                if (hasAfter)
                {
                    SecurityData bData;
                    bData = data;
                    afterDatas.add(bData);
                }
            }
            ArrayList<SecurityData> beforeDatas2;
            ArrayList<SecurityData> afterDatas2;
            for (int i = 0; i < minuteDatas.size(); i++)
            {
                SecurityData data = minuteDatas.get(i);
                SecurityData fData;
                fData = data;
                beforeDatas2.add(fData);
                if (hasAfter)
                {
                    SecurityData bData;
                    bData = data;
                    afterDatas2.add(bData);
                }
            }
            StockService::ConvertXDR(code, 1, &beforeDatas);
            if (hasAfter)
            {
                StockService::ConvertXDR(code, 2, &afterDatas);
            }

            StockService::ConvertXDR(code, 1, &beforeDatas2);
            if (hasAfter)
            {
                StockService::ConvertXDR(code, 2, &afterDatas2);
            }
            for (int i = 0; i < m_charts.size(); i++)
            {
                MyChart *myChart = m_charts.get(i);
                if (myChart->m_cycle == 0)
                {
                    myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, trendDatas, true);
                }
                else if (myChart->m_cycle > 0 && myChart->m_cycle < 1440)
                {
                    ArrayList<SecurityData> sDatas = 0;
                    if (myChart->m_subscription == 1)
                    {
                        sDatas = beforeDatas2;
                    }
                    else if (myChart->m_subscription == 2)
                    {
                        sDatas = afterDatas2;
                    }
                    else
                    {
                        sDatas = minuteDatas;
                    }
                    if (myChart->m_cycle > 1)
                    {
                        ArrayList<SecurityData> newDatas;
                        StockService::multiMinuteSecurityDatas(&newDatas, &sDatas, myChart->m_cycle);
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, newDatas, true);
                    }
                    else
                    {
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, sDatas, true);
                    }
                }
                else if (myChart->m_cycle >= 1440)
                {
                    ArrayList<SecurityData> sDatas = 0;
                    if (myChart->m_subscription == 1)
                    {
                        sDatas = beforeDatas;
                    }
                    else if (myChart->m_subscription == 2)
                    {
                        sDatas = afterDatas;
                    }
                    else
                    {
                        sDatas = dayDatas;
                    }
                    if (myChart->m_cycle == 10080)
                    {
                        ArrayList<SecurityData> newDatas;
                        StockService::getHistoryWeekDatas(&newDatas, &sDatas);
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, newDatas, true);
                    }
                    else if (myChart->m_cycle == 43200)
                    {
                        ArrayList<SecurityData> newDatas;                         
                        StockService::getHistoryMonthDatas(&newDatas, &sDatas);
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, newDatas, true);
                    }
                    else if (myChart->m_cycle == 129600)
                    {
                        ArrayList<SecurityData> newDatas;
                        StockService::getHistorySeasonDatas(&newDatas, &sDatas);
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, newDatas, true);
                    }
                    else if (myChart->m_cycle == 259200)
                    {
                        ArrayList<SecurityData> newDatas;
                        StockService::getHistoryHalfYearDatas(&newDatas, &sDatas);
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, newDatas, true);
                    }
                    else if (myChart->m_cycle == 518400)
                    {
                        ArrayList<SecurityData> newDatas;
                        StockService::getHistoryYearDatas(&newDatas, &sDatas);
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, newDatas, true);
                    }
                    else
                    {
                        myChart->bindHistoryDatas(myChart->m_code, myChart->m_cycle, sDatas, true);
                    }
                }
            }
            dayDatas.clear();
            minuteDatas.clear();
            trendDatas.clear();
            beforeDatas.clear();
            afterDatas.clear();
            beforeDatas2.clear();
            afterDatas2.clear();

            getNative()->invalidate();
        }
    }
    else if (message->m_functionID == FUNCTION_NEWDATA)
    {
        if (message->m_requestID == m_requestID1)
        {
            SecurityLatestData latestData = LatestServiceClient::getLatestData(message->m_body, message->m_bodyLength);
            LatestDiv *latestDiv = (LatestDiv*)findView(L"divLatest");
            latestData.m_requestID = m_requestID1;
            if (m_latestData.m_requestID == m_requestID1)
            {
                for (int i = 0; i < m_charts.size(); i++)
                {
                    MyChart *myChart = m_charts.get(i);
                    if (myChart->m_chart->m_datas.size() > 0)
                    {
                        myChart->newData(&latestData, latestData.m_requestID, true);
                    }
                }
            }
            m_latestData = latestData;
            latestDiv->bindData(latestData, true);
        }
    }
}

void MainFrame::callInvokeEvent(String eventName, Object sender, Object args, Object invoke){
    if (args != 0)
    {
        FCMessage *message = (FCMessage*)args;
        bindData(message);
        delete message->m_body;
        delete message;
    }
}

